/*
 * Copyright 2014 Soichiro Kashima
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.zbbt.sportslottery.ui.adapter;

import java.util.List;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.zbbt.sportslottery.R;
import com.zbbt.sportslottery.app.App;
import com.zbbt.sportslottery.bean.News;
import com.zbbt.sportslottery.bean.NewsStatus;
import com.zbbt.sportslottery.db.DbManager;
import com.zbbt.sportslottery.ui.widget.FooterRecyclerView;
import com.zbbt.sportslottery.ui.widget.recycler.MyItemClickListener;
import com.zbbt.sportslottery.ui.widget.recycler.RecyclerItemViewHolder;
import com.zbbt.sportslottery.util.LogUtil;
import com.zbbt.sportslottery.util.TimeUtil;

public class HomeRecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
	private LayoutInflater mInflater;
	private List<News> mItems;
	private Context myContext;
	private OnBottomClickListener clickLonbottomistener;
	private MyItemClickListener itemClickListener;

	public HomeRecyclerAdapter(List<News> items, Context context) {
		mInflater = LayoutInflater.from(context);
		mItems = items;
		myContext = context;
	}

	@Override
	public int getItemCount() {
		return mItems.size() + 1;
	}

	@Override
	public int getItemViewType(int position) {
		if (position == mItems.size()) {
			return 10;
		} else {
			return mItems.get(position).getImgType();
		}

	}

	@Override
	public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
		// LogUtil.defLog("====onCreateViewHolder=====");
		switch (viewType) {
		case 2:// 纯文本
			return new ItemViewHolder2(mInflater.inflate(R.layout.item_only_text, parent, false),
					itemClickListener);
		case 1:
		case 3:
		case 4:// 小图
			return new ItemViewHolder4(mInflater.inflate(R.layout.item_small_pic, parent, false),
					itemClickListener);
		case 5:// 大图
			return new ItemViewHolder5(mInflater.inflate(R.layout.item_big_pic, parent, false),
					itemClickListener);
		case 6:// 图集
			return new ItemViewHolder6(mInflater.inflate(R.layout.item_atlas, parent, false),
					itemClickListener);
		case 10:// 上拉加载显示
			return new FooterRecyclerView(mInflater.inflate(R.layout.fragment_bottom_layout, parent, false),
					itemClickListener);
		default:
			break;
		}
		return null;
	}

	@Override
	public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, final int position) {
		// LogUtil.defLog("====onBindViewHolder====="+position);
		if (position == mItems.size()) {
			if (clickLonbottomistener != null) {
				clickLonbottomistener.onBottomClick(viewHolder, position);
			}
		} else {
			News itemData = mItems.get(position);
			NewsStatus newsStatus = null;
			newsStatus = DbManager.getReadNews(mItems.get(position).getNewsId() + "");
			String strTimer;
			if (viewHolder instanceof ItemViewHolder2) {
				if (itemData != null) {
					strTimer = TimeUtil.getTimeInterval(itemData.getCreateAt() + "");
					((ItemViewHolder2) viewHolder).tTimer.setText(strTimer);
					((ItemViewHolder2) viewHolder).title.setText(itemData.getNewsTitle());
					((ItemViewHolder2) viewHolder).summary.setText(itemData.getNewsModel()
							.replace("\r\n", ""));
					if (newsStatus != null) {
						((ItemViewHolder2) viewHolder).title.setTextColor(myContext.getResources().getColor(
								R.color.black_54));
						((ItemViewHolder2) viewHolder).summary.setTextColor(myContext.getResources()
								.getColor(R.color.black_54));
					} else {
						((ItemViewHolder2) viewHolder).title.setTextColor(myContext.getResources().getColor(
								R.color.black_87));
						((ItemViewHolder2) viewHolder).summary.setTextColor(myContext.getResources()
								.getColor(R.color.black_54));
					}
					((ItemViewHolder2) viewHolder).information_flag.setText(getNewsFlag(itemData
							.getNews_flag()));
				}
			} else if (viewHolder instanceof ItemViewHolder4) {
				if (itemData != null) {
					((ItemViewHolder4) viewHolder).title.setText(itemData.getNewsTitle());
					strTimer = TimeUtil.getTimeInterval(itemData.getCreateAt() + "");
					if (getNewsFlag(itemData.getNews_flag()).equals("")) {
						((ItemViewHolder4) viewHolder).information_time.setText(strTimer);
					} else {
						((ItemViewHolder4) viewHolder).information_time.setText(strTimer + "  "
								+ getNewsFlag(itemData.getNews_flag()));
					}
					if (newsStatus != null) {
						((ItemViewHolder4) viewHolder).title.setTextColor(myContext.getResources().getColor(
								R.color.black_54));
						((ItemViewHolder4) viewHolder).information_time.setTextColor(myContext.getResources()
								.getColor(R.color.black_54));
					} else {
						((ItemViewHolder4) viewHolder).title.setTextColor(myContext.getResources().getColor(
								R.color.black_87));
						((ItemViewHolder4) viewHolder).information_time.setTextColor(myContext.getResources()
								.getColor(R.color.black_54));
					}
					// if(((ItemViewHolder4)
					// viewHolder).smallPic.getTag()==null){
//					LogUtil.defLog(((ItemViewHolder4) viewHolder).smallPic.getTag() + "-----");
					if (null != mItems.get(position).getNewsImg() && itemData.getNewsImg().size() > 0) {
						ImageLoader.getInstance().displayImage(itemData.getNewsImg().get(0).getImgUrl() + "",
								((ItemViewHolder4) viewHolder).smallPic, App.option);
						((ItemViewHolder4) viewHolder).smallPic.setTag(itemData.getNewsImg().get(0)
								.getImgUrl());
					} else {
						((ItemViewHolder4) viewHolder).smallPic.setImageResource(R.drawable.img_load_fail);
					}
				}

				// }
			} else if (viewHolder instanceof ItemViewHolder5) {
				if (itemData != null) {
					((ItemViewHolder5) viewHolder).title.setText(itemData.getNewsTitle());
					strTimer = TimeUtil.getTimeInterval(itemData.getCreateAt() + "");
					if (getNewsFlag(itemData.getNews_flag()).equals("")) {
						((ItemViewHolder5) viewHolder).bigTimer.setText(strTimer);
					} else {
						((ItemViewHolder5) viewHolder).bigTimer.setText(strTimer + "  "
								+ getNewsFlag(itemData.getNews_flag()));
					}
					if (newsStatus != null) {
						((ItemViewHolder5) viewHolder).title.setTextColor(myContext.getResources().getColor(
								R.color.black_54));
					} else {
						((ItemViewHolder5) viewHolder).title.setTextColor(myContext.getResources().getColor(
								R.color.black_87));
					}
//					if (((ItemViewHolder5) viewHolder).bitPic.getTag() == null) {
						if (null != itemData.getNewsImg() && itemData.getNewsImg().size() > 0) {
							ImageLoader.getInstance().displayImage(
									itemData.getNewsImg().get(0).getImgUrl() + "",
									((ItemViewHolder5) viewHolder).bitPic, App.option);
							((ItemViewHolder5) viewHolder).bitPic.setTag(itemData.getNewsImg().get(0)
									.getImgUrl());
						} else {
							((ItemViewHolder5) viewHolder).bitPic.setImageResource(R.drawable.img_load_failbig);
						}
					}
//				}
			} else if (viewHolder instanceof ItemViewHolder6) {
				if (itemData != null) {
					((ItemViewHolder6) viewHolder).title.setText(itemData.getNewsTitle());
					strTimer = TimeUtil.getTimeInterval(itemData.getCreateAt() + "");
					if (getNewsFlag(itemData.getNews_flag()).equals("")) {
						((ItemViewHolder6) viewHolder).atTimer.setText(strTimer);
					} else {
						((ItemViewHolder6) viewHolder).atTimer.setText(strTimer + "  "
								+ getNewsFlag(itemData.getNews_flag()));
					}
					if (newsStatus != null) {
						((ItemViewHolder6) viewHolder).title.setTextColor(myContext.getResources().getColor(
								R.color.black_54));
					} else {
						((ItemViewHolder6) viewHolder).title.setTextColor(myContext.getResources().getColor(
								R.color.black_87));
					}
					if (null != itemData.getNewsImg() && itemData.getNewsImg().size() > 0) {
						ImageLoader.getInstance().displayImage(itemData.getNewsImg().get(0).getImgUrl() + "",
								((ItemViewHolder6) viewHolder).picLeft, App.option);
						ImageLoader.getInstance().displayImage(itemData.getNewsImg().get(1).getImgUrl() + "",
								((ItemViewHolder6) viewHolder).picMiddle, App.option);
						ImageLoader.getInstance().displayImage(itemData.getNewsImg().get(2).getImgUrl() + "",
								((ItemViewHolder6) viewHolder).picRight, App.option);
					}
				}
			}
		}

	}

	static class ItemViewHolder2 extends RecyclerItemViewHolder {
		TextView tTimer;
		TextView title;
		TextView summary;
		TextView information_flag;

		public ItemViewHolder2(View view, MyItemClickListener itemClickListener) {
			super(view, itemClickListener);
			title = (TextView) view.findViewById(R.id.information_only_text_title);
			summary = (TextView) view.findViewById(R.id.information_summary);
			tTimer = (TextView) view.findViewById(R.id.information_timer);
			information_flag = (TextView) view.findViewById(R.id.information_flag);
		}
	}

	static class ItemViewHolder4 extends RecyclerItemViewHolder {
		TextView title;
		ImageView smallPic;
		TextView information_time;

		public ItemViewHolder4(View view, MyItemClickListener itemClickListener) {
			super(view, itemClickListener);
			title = (TextView) view.findViewById(R.id.information_small_pic_title);
			smallPic = (ImageView) view.findViewById(R.id.information_small_pic);
			information_time = (TextView) view.findViewById(R.id.information_time);
		}
	}

	static class ItemViewHolder5 extends RecyclerItemViewHolder {
		TextView bigTimer;
		TextView title;
		ImageView bitPic;

		public ItemViewHolder5(View view, MyItemClickListener itemClickListener) {
			super(view, itemClickListener);
			title = (TextView) view.findViewById(R.id.information_big_pic_title);
			bitPic = (ImageView) view.findViewById(R.id.information_big_pic);
			bigTimer = (TextView) view.findViewById(R.id.information_big_timer);
		}
	}

	static class ItemViewHolder6 extends RecyclerItemViewHolder {
		TextView atTimer;
		TextView title;
		ImageView picLeft;
		ImageView picMiddle;
		ImageView picRight;

		public ItemViewHolder6(View view, MyItemClickListener itemClickListener) {
			super(view, itemClickListener);
			title = (TextView) view.findViewById(R.id.information_atlas_title);
			picLeft = (ImageView) view.findViewById(R.id.information_atlas_img_left);
			picMiddle = (ImageView) view.findViewById(R.id.information_atlas_img_middle);
			picRight = (ImageView) view.findViewById(R.id.information_atlas_img_right);
			atTimer = (TextView) view.findViewById(R.id.information_atls_timer);
		}
	}

	public String getNewsFlag(int num) {
		if (num == 1) {
			return "热";
		} else if (num == 2) {
			return "推荐";
		} else if (num == 3) {
			return "专家";
		}
		return "";
	}

	public interface OnBottomClickListener {
		public void onBottomClick(RecyclerView.ViewHolder holder, int position);
	}

	public void setOnBottomClickListener(OnBottomClickListener clickListener) {
		this.clickLonbottomistener = clickListener;
	}

	/**
	 * 设置Item点击监听
	 * 
	 * @param listener
	 */
	public void setOnItemClickListener(MyItemClickListener listener) {
		this.itemClickListener = listener;
	}
}
